{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5a465904",
   "metadata": {},
   "source": [
    "### 本周内容\n",
    "#### 1. 解读互联网常用数据Json与字典\n",
    "#### 2. 字典的实践（综合）\n",
    "#### 3. 集合\n",
    "#### 4.数据结构综合实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fca8ab40",
   "metadata": {},
   "source": [
    "### 3.集合\n",
    "> 1. [集合]( https://docs.python.org/3/tutorial/datastructures.html#sets)的概念\n",
    "> 2. 集合的形式：{value1,value2,value3,...}\n",
    "> 3. 集合的性质:\n",
    ">> 1.唯一性（去除重复项：一般可以用来查看列表的成员属性）\n",
    ">> 2.可变性\n",
    "> 4.集合可以用 in 进行成员查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eb64ce9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'set'> \n",
      " {'banana', 'orange', 'apple', 'pear'}\n"
     ]
    }
   ],
   "source": [
    "basket = {'apple','orange','apple','pear','orange','banana'}\n",
    "print(type(basket),'\\n',basket)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39b07055",
   "metadata": {},
   "source": [
    "### 3.1 集合的运算\n",
    "> 1.交集运算 intersection()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "90f02f7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6, 7}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list_1 = [1,2,5,6,7,2,3,2,4,4]\n",
    "set_1 = set(list_1)  # list to set 的方法 set(list)\n",
    "set_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8ebc0d60",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 3, 4, 7, 8, 66}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_2 = set([1,3,4,0,8,66,7])\n",
    "set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "857535fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function intersection:\n",
      "\n",
      "intersection(...) method of builtins.set instance\n",
      "    Return the intersection of two sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in both sets.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.intersection)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b34d0820",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 3, 4, 7}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法一：\n",
    "new_intersection = set_1.intersection(set_2)\n",
    "new_intersection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e0b539c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法二：\n",
    "set_1 & set_2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee4adcd6",
   "metadata": {},
   "source": [
    "> 3.2 集合的并集运算 \n",
    "> 1.union()\n",
    "> 2. set1 | set2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b8b886dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function union:\n",
      "\n",
      "union(...) method of builtins.set instance\n",
      "    Return the union of sets as a new set.\n",
      "    \n",
      "    (i.e. all elements that are in either set.)\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_1.union)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0eb42450",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6, 7}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c68b4bd7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 3, 4, 7, 8, 66}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6dae3389",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 2, 3, 4, 5, 6, 7, 8, 66}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法一：\n",
    "set_1.union(set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "326b5522",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法二：\n",
    "set_1 | set_2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cee0e49",
   "metadata": {},
   "source": [
    "> 3. 差集运算 注：差集运算考虑先后顺序\n",
    "> 1. set1.difference(set2)\n",
    "> 2. set1 - set2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8195e08d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6, 7}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ea76b40e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 3, 4, 7, 8, 66}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6b98230c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{2, 5, 6}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_1.difference(set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "dccabb73",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 8, 66}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_2.difference(set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "434e21a9",
   "metadata": {},
   "source": [
    "> 4. 对称差集 symmetric difference()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cb2e4a2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 2, 3, 4, 5, 6, 7}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "874d8821",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1, 3, 4, 7, 8, 66}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8bb60a03",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 2, 5, 6, 8, 66}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_1.symmetric_difference(set_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a7e82d3",
   "metadata": {},
   "source": [
    "### 3.2 集合的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e56415d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "set_3 = set([1,3,4,0,8,66,7])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1b51d6e",
   "metadata": {},
   "source": [
    "> 1. 增加 add()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "556fb976",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function add:\n",
      "\n",
      "add(...) method of builtins.set instance\n",
      "    Add an element to a set.\n",
      "    \n",
      "    This has no effect if the element is already present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_3.add)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2b3a32bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8, 9}\n"
     ]
    }
   ],
   "source": [
    "set_3.add(9)\n",
    "print(set_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c1459bac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 66, 3, 4, 7, 8, 9}\n"
     ]
    }
   ],
   "source": [
    "set_3.add(66)\n",
    "print(set_3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f2c18dd8",
   "metadata": {},
   "source": [
    "> 2. 删除（先用 in 查看数据是否存在）\n",
    ">> 1. pop(）：随即删除成员 Remove and return an arbitrary(任意的) set element\n",
    ">> 2. remove(member):指定删除成员\n",
    ">> 3. discard(member) 指定删除成员：不会报 KeyError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f3f4fd48",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(...) method of builtins.set instance\n",
      "    Remove and return an arbitrary set element.\n",
      "    Raises KeyError if the set is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_3.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ec5cff93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_3.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0851dc99",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 66, 3, 4, 7, 8, 9}\n"
     ]
    }
   ],
   "source": [
    "print(set_3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "54d67ee0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(...) method of builtins.set instance\n",
      "    Remove an element from a set; it must be a member.\n",
      "    \n",
      "    If the element is not a member, raise a KeyError.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_3.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a7101288",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 3, 4, 7, 8, 9}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 66 in set_3:\n",
    "    set_3.remove(66)\n",
    "set_3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "7609c32f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function discard:\n",
      "\n",
      "discard(...) method of builtins.set instance\n",
      "    Remove an element from a set if it is a member.\n",
      "    \n",
      "    If the element is not a member, do nothing.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(set_3.discard)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "74b8c025",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1, 3, 4, 7, 8}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set_3.discard(9)\n",
    "set_3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57740557",
   "metadata": {},
   "source": [
    "#### 课本实践（集合）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "dbf08ae4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:Juicy\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'i', 'u'}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = vowels.intersection(set(word))\n",
    "found"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
